package com.ks.phone.wedget;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ks.basictools.utils.ToastUtils;
import com.ks.phone.R;
import com.ks.phone.UserManager;
import com.ks.phone.constant.Constants;
import com.ks.phone.databinding.DialogDetailBinding;
import com.ks.phone.manager.PreferenceManager;
import com.ks.phone.responseBean.CustomerDetail;
import com.ks.phone.responseBean.CustomerList;
import com.ks.phone.responseBean.UserResponse;
import com.ks.phone.view.CustomerActivity;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;

import java.util.HashMap;
import java.util.Map;

import razerdp.basepopup.BasePopupWindow;
import razerdp.util.animation.AnimationHelper;
import razerdp.util.animation.ScaleConfig;

public class PopupDetail extends BasePopupWindow {

    private DialogDetailBinding mBinding;
    private CustomerList.ListDTO.DataDTO data;

    public final ObservableField<String> number = new ObservableField<>();
    public final ObservableField<String> second = new ObservableField<>();

    public PopupDetail(Context context) {
        super(context);
        setContentView(R.layout.dialog_detail);
        setPopupGravity(Gravity.CENTER);
        setAutoMirrorEnable(true);      // 如果当前屏幕不足以完整显示您的PopupWindow，则PopupWindow会自行布置在其镜像位置。
        setBlurBackgroundEnable(false); // 设置PopupWindow弹出时是否模糊背景
//        setBackground(null);            // 设置无背景
        setOutSideDismiss(true);
        setBackPressEnable(true);
        setOutSideTouchable(true);
    }

    public CustomerList.ListDTO.DataDTO getData() {
        return data;
    }

    public void setData(CustomerList.ListDTO.DataDTO data) {
        this.data = data;
        if (data != null) {
            requestData();
        }
    }

    @Override
    public void onViewCreated(@NonNull View contentView) {
        mBinding = DialogDetailBinding.bind(contentView);
        mBinding.setPopup(this);
    }

    private void requestData() {
        UserResponse.UserDTO user = UserManager.getInstance().getUser();
        Map<String, Object> param = new HashMap<>();
        param.put("userid", user.getId());
        param.put("token", PreferenceManager.getInstance().getToken(getContext()));
        param.put("cid", data.getCid());
        XHttp.post(Constants.URL_CUSTOMER_INFO)
                .params(param)
                .execute(new SimpleCallBack<CustomerDetail>() {
                    @Override
                    public void onSuccess(CustomerDetail detail) {
                        Log.d("zhxk", "onSuccess: list = " + detail);
                        number.set(detail.getInfo().getNumber() + "次");
                        second.set(detail.getInfo().getSeconds() + "秒");
                    }

                    @Override
                    public void onError(ApiException e) {
                        e.printStackTrace();
                        Log.e("zhxk", "onError: e = " + e.getMessage());
                        ToastUtils.show(getContext(), e.getMessage());
                    }
                });
    }

    @Override
    protected Animation onCreateShowAnimation() {
        return AnimationHelper.asAnimation()
                .withScale(ScaleConfig.CENTER)
                .toShow();
    }

    @Override
    protected Animation onCreateDismissAnimation() {
        return AnimationHelper.asAnimation()
                .withScale(ScaleConfig.CENTER)
                .toDismiss();
    }

}
